package UI;

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;

public class GameJFrame extends JFrame implements KeyListener, ActionListener, MouseListener {
    //游戏窗口
    //设置二维数组管理空白
    String [][]image = new String[4][4];
    //记录空白的位置
    private int x = 0;
    private int y = 0;
    //随机图片
    int rand = new Random().nextInt(new PicturePackage().entirePictures.length);
    String [][]win = transformOne_dimensional_to_Two(new PicturePackage().pictures[rand]);
    //计步
    private int count = 0;
    //设置菜单
    //JMenuBar->菜单栏
    //JMenu->菜单
    JMenuBar menu = new JMenuBar();
    JMenu function = new JMenu("功能");
    JMenu help = new JMenu("帮助");
    JMenu recharge = new JMenu("充值系统(doge)");
    //function目录
    JMenu changePicture = new JMenu("更换图片");
    JMenuItem view = new JMenuItem("风景");
    JMenuItem kongS = new JMenuItem("坎公");
    JMenuItem Cartoon = new JMenuItem("二次螈");
    JMenuItem replay = new JMenuItem("重新开始");
    JMenuItem reLogin = new JMenuItem("重新登录");
    JMenuItem close = new JMenuItem("退出游戏");
    public GameJFrame() {
        //初始化图片
        initImage();
        changeImage();
        //初始化背景
        initBackground();
        //初始化菜单
        initMenu();
        //初始化/加载界面
        initFrame();
    }

    private void initImage() {
        //初始化图片
        String []imageArray = new PicturePackage().pictures[rand];
        disrupt(imageArray);
        //转为二维数组
        for (int i = 0; i < imageArray.length; i++) {
            if (imageArray[i] == "0") {
                //把空白方块的引索赋值给x,y
                x = i / 4;
                y = i % 4;
            } else {
                image[i / 4][i % 4] = imageArray[i];
            }
        }
    }
    private void initBackground() {
        //设置背景
        JLabel jLabel = new JLabel(new ImageIcon("image\\Background.png"));
        jLabel.setBounds(-10, -35, 900, 880);
        this.getContentPane().add(jLabel);
    }

    private void initFrame() {
        //标题
        this.setTitle("拼图游戏1.5");
        //界面大小
        this.setSize(900, 880);
        //界面置顶(未启用)
        //this.setAlwaysOnTop(true);
        //界面居中
        this.setLocationRelativeTo(null);
        //关闭图片默认居中放置
        this.setLayout(null);
        //界面关闭结束程序
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        //界面显示
        this.setVisible(true);
        //添加键盘监听
        this.addKeyListener(this);
    }

    private void initMenu() {
        changePicture.add(view);
        changePicture.add(kongS);
        changePicture.add(Cartoon);
        function.add(changePicture);
        function.add(replay);
        function.add(reLogin);
        function.add(close);
        //菜单添加
        menu.add(function);
        menu.add(help);
        menu.add(recharge);
        //为菜单设置鼠标监听
        view.addActionListener(this);
        kongS.addActionListener(this);
        Cartoon.addActionListener(this);
        replay.addActionListener(this);
        reLogin.addActionListener(this);
        close.addActionListener(this);
        help.addMouseListener(this);
        recharge.addMouseListener(this);
        this.setJMenuBar(menu);
    }

    //重新加载图片
    private void changeImage() {
        //移去所有图片
        this.getContentPane().removeAll();
        //判断胜利
        if (victory()) {
            JLabel win = new JLabel(new ImageIcon("image\\win.jpg"));
            win.setBounds(73, 200, 740, 390);
            this.getContentPane().add(win);
        }
        JLabel stepCount = new JLabel("总步数: " + count);
        stepCount.setBounds(70, 0, 150, 50);
        this.getContentPane().add(stepCount);
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                //循环添加每一张图片
                JLabel jLabel = new JLabel(new ImageIcon(image[i][j]));
                //初始化图片位置
                jLabel.setBounds(85 + (180 * j), 50 + (180 * i), 180,180);
                //给图片加边框
                jLabel.setBorder(new LineBorder(Color.white));
                this.getContentPane().add(jLabel);
            }
        }
        //刷新图片
        this.getContentPane().repaint();
    }
    //打乱图片方法
    private static void disrupt(String[] imageArray) {
        //打乱图片
        Random ran = new Random();
        for (int i = 0; i < imageArray.length; i++) {
            int j = ran.nextInt(imageArray.length);
            String t = imageArray[i];
            imageArray[i] = imageArray[j];
            imageArray[j] = t;
        }
        int rand = ran.nextInt(16);
        imageArray[rand] = "0";
    }

    //胜利方法
    private boolean victory() {
        int sum = 0;
        for (int i = 0; i < image.length; i++) {
            for (int j = 0; j < image[i].length; j++) {
                if(image[i][j] == win[i][j]) {
                    sum++;
                }
            }
        }
        return sum >= 15;
    }
    @Override
    public void keyTyped(KeyEvent e) {

    }
    @Override
    public void keyPressed(KeyEvent e) {
        final int code = e.getKeyCode();
        if (code == 10) {
            //显示全图
            this.getContentPane().removeAll();
            JLabel jLabel = new JLabel(new ImageIcon(new PicturePackage().entirePictures[rand]));
            jLabel.setBounds(85, 50, 720, 720);
            this.getContentPane().add(jLabel);
            initBackground();
            this.getContentPane().repaint();
        }
    }
    @Override
    public void keyReleased(KeyEvent e) {
        if (victory()) {
            //胜利则结束此方法
            return;
        }
        final int code = e.getKeyCode();
        if (code == 68 || code == 39) {
            //右移
            if (y - 1 >= 0) {
                image[x][y] = image[x][y - 1];
                image[x][y - 1] = "0";
                y--;
                count++;
                changeImage();
                initBackground();
            } else {
                return;
            }
        } else if (code == 65 || code == 37) {
            //左移
            if (y + 1 <= 3) {
                image[x][y] = image[x][y + 1];
                image[x][y + 1] = "0";
                y++;
                count++;
                changeImage();
                initBackground();
            } else {
                return;
            }
        } else if (code == 87 || code == 38) {
            //上移
            if (x + 1 <= 3) {
                image[x][y] = image[x + 1][y];
                image[x + 1][y] = "0";
                x++;
                count++;
                changeImage();
                initBackground();
            } else {
                return;
            }
        } else if (code == 83 || code == 40) {
            //下移
            if (x - 1 >= 0) {
                image[x][y] = image[x - 1][y];
                image[x - 1][y] = "0";
                x--;
                count++;
                changeImage();
                initBackground();
            } else {
                return;
            }
        } else if (code == 10) {
            changeImage();
            initBackground();
            return;
        } else if (code == 524) {
            //一键通关
            this.getContentPane().removeAll();
            String []imageArray = new PicturePackage().pictures[rand];
            for (int i = 0; i < imageArray.length; i++) {
                image[i / 4][i % 4] = imageArray[i];
            }
            changeImage();
            initBackground();
        }
    }
    //一二维数组转换器
    private String[][] transformOne_dimensional_to_Two(String[] Array) {
        String[][] arr = new String[4][4];
        for (int i = 0; i < Array.length; i++) {
            arr[i / 4][i % 4] = Array[i];
        }
        return arr;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == replay) {
            //重开
            initImage();
            image[x][y] = "0";
            this.count = 0;
            changeImage();
            initBackground();
        } else if (e.getSource() == reLogin) {
            //重新登录 —— new一个对象
            this.setVisible(false);
            new LoginJFrame();
        } else if (e.getSource() == view || e.getSource() == kongS || e.getSource() == Cartoon) {
            //更换图片
            Random random = new Random();
            if (e.getSource() == view) {
                rand = random.nextInt( 2) + 8;
            } else if (e.getSource() == kongS) {
                rand = random.nextInt( 2);
            } else if (e.getSource() == Cartoon) {
                rand = random.nextInt(6) + 2;
            }
            initImage();
            image[x][y] = "0";
            win = transformOne_dimensional_to_Two(new PicturePackage().pictures[rand]);
            this.count = 0;
            changeImage();
            initBackground();
        } else if (e.getSource() == close) {
            //关闭虚拟机
            System.exit(0);
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getSource() == help) {
            JDialog jDialog = new JDialog();
            JLabel help = new JLabel(new ImageIcon("image\\help.jpg"));
            jDialog.setBounds(0, 0, 650, 420);
            help.setBounds(0, 0, 650, 420);
            jDialog.setTitle("帮助");
            jDialog.getContentPane().add(help);
            jDialog.setLocationRelativeTo(null);
            jDialog.setModal(true);
            jDialog.setVisible(true);
        } else if (e.getSource() == recharge ) {
            JDialog jDialog = new JDialog();
            JLabel V50 = new JLabel(new ImageIcon("image\\V50.jpg"));
            jDialog.setBounds(0, 0, 600, 800);
            V50.setBounds(0, 0, 600, 800);
            jDialog.setTitle("你好，V我50，我告诉你答案");
            jDialog.getContentPane().add(V50);
            jDialog.setLocationRelativeTo(null);
            jDialog.setModal(true);
            jDialog.setVisible(true);
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
